package com.t114514.demo.controller;

import com.t114514.demo.pojo.*;
import com.t114514.demo.service.QuestionnaireAnswerService;
import com.t114514.demo.service.QuestionnaireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static java.lang.Character.getNumericValue;

@RestController
@RequestMapping("/dataAnalysis")
public class DataAnalysisController {

    @Autowired
    private QuestionnaireAnswerService questionnaireAnswerService;
    @Autowired
    private QuestionnaireService questionnaireService;

    @GetMapping("/averageScores")
    public Result<List<Score>> getAverageScoresByQuestionnaireId(@RequestParam int questionnaireId,int teacherId) {

        // 获取该问卷下所有问题的列表
        List<Question> questions = questionnaireService.findQuestionsByQuestionnaireId(questionnaireId);

        // 初始化一个Map来存储问题和平均分
        List<Score> averageScores = new ArrayList<>();;

        // 遍历每个问题，计算平均分
        for (Question question : questions) {
            double totalScore = 0;
            int Count = 0;
            List<Answer> answers = questionnaireAnswerService.findAnswersByQuestionId(question.getQuestionId(),teacherId);
            for (Answer answer : answers) {
                List<Options> options = questionnaireService.findOptionsByQuestionId(question.getQuestionId());
                for (Options option : options) {
                    if (option.getOptionNumber()==Integer.parseInt((answer.getAnswerContent()))) {
                        totalScore += option.getOptionValue();
                    }
                }
                Count++;
            }

            // 计算平均分并存储到Map中
            if (Count > 0) {
                double averageScore = totalScore / Count;
                Score score = new Score(question.getQuestionNumber(),question.getQuestionContent(),averageScore);
                averageScores.add(score);
            } else {
                Score score = new Score(question.getQuestionNumber(),question.getQuestionContent(),0.0);
                averageScores.add(score); // 没有答案时平均分为0
            }
        }

        // 返回结果
        return Result.success(averageScores);
    }

    @GetMapping("/getAnswerContent")
    public Result<PageBean<AnswerContent<Answer>>> getContentByQuestionnaireId(@RequestParam int questionnaireId,int teacherId,int pageNum,int pageSize) {

        PageBean<AnswerContent<Answer>> answerContents = questionnaireService.AnswerContent(questionnaireId,teacherId,pageNum,pageSize);

        // 返回结果
        return Result.success(answerContents);
    }



//    // 获取该问卷下所有问题的列表
//        List<Question> questions = questionnaireService.findQuestionsByQuestionnaireId2(questionnaireId);
//
//        // 初始化一个列表来存储 AnswerContent 对象
//        List<AnswerContent<Answer>> answerContents = new ArrayList<>();
//
//        // 遍历每个问题，获取答案并封装到 AnswerContent 对象中
//        for (Question question : questions) {
//            // 获取该问题的所有答案
//            List<Answer> answers = questionnaireAnswerService.findAnswersByQuestionId(question.getQuestionId());
//
//            // 创建 AnswerContent 对象
//            AnswerContent<Answer> answerContent = new AnswerContent<>(question.getQuestionNumber(), question.getQuestionContent(), answers);
//
//            // 将 AnswerContent 对象添加到列表中
//            answerContents.add(answerContent);
//        }




}

